﻿using ParallelLib.Micro;
using System;

namespace PaConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //FindService.Instance.Init();
            //FindService.Instance.SayHello();
            //FindService.Instance.StartListenMsg();
            //while (true)
            //{
            //    var msg = Console.ReadLine();
            //    switch (msg)
            //    {
            //        case "q":
            //            FindService.Instance.QueryAllNode();
            //            break;
            //        default:
            //            break;
            //    }
            //}
            var id = "用户-" + Environment.TickCount % 10000;
            var service = new MicroService(id, 12888);
            service.InitNode();
            service.StartListenMsg();
            Console.Title = service.HostAddress + "--->" + id;
            service.QueryAllNode();
            Console.WriteLine();
            while (true)
            {
                var msg = Console.ReadLine().ToLower();
                switch (msg)
                {
                    case "q":
                        service.QueryAllNode();
                        break;
                    case "p":
                        Console.WriteLine("请输入发布的信息:");
                        var msg2 = Console.ReadLine();
                        service.PublishMsg(msg2);
                        Console.WriteLine("发布完成。");
                        break;
                    case "s":
                        service.QueryAllNode();
                        var index = Console.ReadLine();
                        Console.WriteLine("请输入的信息:");
                        var msgx = Console.ReadLine();
                        int x = -1;
                        int.TryParse(index, out x);
                        if (x >= 0)
                        {
                            service.SayTo(service.GetNodeByIndex(x), msgx);
                            Console.WriteLine("发送完毕.");
                        }
                        break;
                    case "hello":
                        service.Hello();
                        break;

                    default:
                        if(msg.StartsWith("s "))
                        {
                            var pars = msg.Split(' ');
                            int t = int.Parse(pars[1]);
                            service.SayTo(service.GetNodeByIndex(t), pars[2]);
                        }
                        break;
                }
            }
        }
    }
}
